package com.synjones.zeuscloudweb.service.impl;

import com.synjones.zeus.model.web.MenuBean;
import com.synjones.zeuscloudweb.dao.MenuDaoMapper;
import com.synjones.zeuscloudweb.service.MenuService;
import com.synjones.zeuscloudweb.util.ccl.datastructure.impl.BaseDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import static com.synjones.zeus.model.CodeStr.*;

/**
 * @author  yys
 * Description:
 * @date  created in 10:40 2018-01-15
 * Modified by:
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Resource
    private MenuDaoMapper menuDaoMapper;

    public static final int rootLength2 = 2;
    public static final int rootLength3 = 3;

 /**
   *  添加菜单
   *  @param menuBean 菜单
   *  @author yys
   *  Description:  添加菜单
   *  @date  12:08 2018-01-15
   *  @return BaseDto
   *  throws
   */
    @Override
    @Transactional
    public BaseDto insertMenu(MenuBean menuBean) {
        BaseDto jsonDto = new BaseDto();
        try{
            String maxId = menuDaoMapper.selectMaxIdByPid(menuBean.getpId());
            if(maxId == null){
                maxId =  menuBean.getpId() + NUM1;
                menuBean.setId(maxId);
            }else{
                menuBean.setId(NUM0 + String.valueOf(Integer.valueOf(maxId) + 1));
            }
            if(menuBean.getpId().length() == rootLength2){
                menuBean.setType(NUM1);
            }else{
                menuBean.setType(NUM2);
            }
           menuDaoMapper.insertMenu(menuBean);
            jsonDto.put("success", new Boolean(true));
            jsonDto.put("msg", "操作成功!");
            jsonDto.put("data",null);
        }catch (Exception e){
            jsonDto.put("success", new Boolean(false));
            jsonDto.put("msg", "操作失败!");
            jsonDto.put("data",null);
        }
        return jsonDto;
    }
 /**
   *  获取菜单
   *  @param Id 菜单Id
   *  @author yys
   *  Description:  获取菜单
   *  @date  15:16 2018-01-16
   *  @return  MenuBean
   *  throws
   */
    @Override
    public MenuBean selectMenu(String Id) {
        return menuDaoMapper.selectMenu(Id);
    }
  /**
   *  修改菜单
   *  @param menuBean 菜单
   *  @author yys
   *  Description:  修改菜单
   *  @date  15:16 2018-01-16
   *  @return BaseDto
   */
    @Override
    public BaseDto updateMenu(MenuBean menuBean) {
        BaseDto jsonDto = new BaseDto();
        try{
            menuDaoMapper.updateMenu(menuBean);
            jsonDto.put("success", new Boolean(true));
            jsonDto.put("msg", "操作成功!");
            jsonDto.put("data",null);
        }catch (Exception e){
            jsonDto.put("success", new Boolean(false));
            jsonDto.put("msg", "操作失败!");
            jsonDto.put("data",null);
        }
        return jsonDto;
    }
  /**
   * 删除菜单
   * @param Id 菜单Id
   * @author yys
   * description:  删除菜单
   * @date  15:02 2018-01-08
   * return: BaseDto
   */
    @Override
    @Transactional
    public BaseDto deleteMenuById(String Id) {
        BaseDto jsonDto = new BaseDto();
        try{
            if(Id.length() == rootLength3){
                //删除节点
                menuDaoMapper.deleteMenuById(Id);
                //删除节点下子节点
                menuDaoMapper.deleteMenuByPid(Id);
            }else{
                //删除节点
                menuDaoMapper.deleteMenuById(Id);
            }
            jsonDto.put("success", new Boolean(true));
            jsonDto.put("msg", "操作成功!");
            jsonDto.put("data",null);
        }catch (Exception e){
            jsonDto.put("success", new Boolean(false));
            jsonDto.put("msg", "操作失败!");
            jsonDto.put("data",null);
        }
        return jsonDto;
    }
}
